//
//  HMNetworkTools.m
//  OC网络请求封装
//
//  Created by HM on 16/9/20.
//  Copyright © 2016年 HM. All rights reserved.
//

#import "HMNetworkTools.h"

@implementation HMNetworkTools

+ (instancetype)sharedTools {
    
    static HMNetworkTools *tools;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[HMNetworkTools alloc] init];
        //  设置可以接受的响应类型的数据
        tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        
        tools.requestSerializer = [AFJSONRequestSerializer serializer];
        [tools.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        
    });
    
    return tools;

}

- (void)requestWithType: (RequestType)type andUrl: (NSString *)url andParams: (id)params andCallBack: (void (^) (id response, NSError *error))callBack {
    
    if (type == GET) {
        [self GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            callBack(nil, error);
        }];
        
    } else {
        
        [self POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            callBack(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            callBack(nil, error);
        }];
        
    }
    
    

    

}



@end
